'use strict';

const BaseController = require('../base');
const { Op } = require('sequelize');
class BannerController extends BaseController {
  static get serviceName() {
    return 'business.product';
  }

  async search() {
    let keyword = this.ctx.query.keyword;
    if (!keyword) {
      this.ctx.success({productList: [], newsList: []});
      return
    }
    let productList = await this.ctx.model.Business.Product.findAll({
      where: {
        // 标题或者内容
        title: { [Op.like]: `%${keyword}%` },
      }
    });
    let newsList = await this.ctx.model.Business.News.findAll({
      where: {
        title: { [Op.like]: `%${keyword}%` },
      },
      include: [
        {
          model: this.ctx.model.Business.Comment,
          as: 'comments',
          where: {
            type: 'news'
          },
          required: false
        },
        {
          model: this.ctx.model.Business.Like,
          as: 'likes',
          where: {
            type: 'news'
          },
          required: false
        },
        {
          model: this.ctx.model.Business.Collection,
          as: 'collections',
          where: {
            type: 'news'
          },
          required: false
        },
      ]
    });
    this.ctx.success({productList, newsList});
  }
}

module.exports = BannerController;
